package com.abcdefg.redisdemo.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.params.SetParams;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisDemo {

    public static void test(Jedis jedis) {
//        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
//        //清空redis数据库
//        Jedis jedis = jedisPool.getResource();
//        jedis.flushDB();

        System.out.println("get 和 set ");
        //先清空redis数据库
        jedis.flushAll();

//        jedis.set("key", "111");
        jedis.set("key2", "222");

        SetParams params = new SetParams();
        params.xx();
        params.ex(10);
        jedis.set("key2", "333", params);
//        String key = jedis.get("key");
//        System.out.println("key="+key);
        String key2 = jedis.get("key2");
        System.out.println("key2=" + key2);

    }

    public static void test2(Jedis jedis) {
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key1", "111");
        jedis.set("key2", "222");

        System.out.println("key1=" + jedis.get("key1"));
        System.out.println("key2=" + jedis.get("key2"));

        boolean key1 = jedis.exists("key1");
        System.out.println("key1:" + key1);

        jedis.del("key1", "key2");
        key1 = jedis.exists("key1");
        System.out.println("key1:" + key1);
        System.out.println("key2:" + jedis.exists("key2"));
    }

    public static void test3(Jedis jedis) {
        System.out.println("expire 和 ttl");
        jedis.flushAll();

        jedis.set("key1", "111");
        jedis.set("key2", "222");
        jedis.expire("key1", 10);

        System.out.println("key1=" + jedis.ttl("key1"));
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long key1 = jedis.ttl("key1");
        System.out.println("key1:" + key1);

    }

    public static void test4(Jedis jedis) {
        System.out.println("type");
        jedis.flushAll();

        jedis.set("key1", "value");
        System.out.println("key1=" + jedis.type("key1"));

        jedis.lpush("key2", "a", "b", "c");
        System.out.println("key2=" + jedis.type("key2"));

        jedis.hset("key3", "name", "张三");
        System.out.println("key3=" + jedis.type("key3"));

        jedis.sadd("key4", "aaa", "bbb", "ccc");
        System.out.println("key4=" + jedis.type("key4"));

        jedis.zadd("key5", 90, "张飞");
        System.out.println("key5=" + jedis.type("key5"));
        HashMap<String, Double> map = new HashMap<>();
        map.put("lvbu", 99.0);
        map.put("zhaoyun", 98.0);
        map.put("guanyv", 97.0);
        String key6 = "myzset";
        jedis.zadd(key6, map);
        System.out.println("key6=" + jedis.type("myzset"));

    }

    //String操作
    public static void test5(Jedis jedis) {
        System.out.println("mget 和 mset");
        jedis.flushAll();

        jedis.mset("key1", "value1", "key2", "value2");
        List<String> mget = jedis.mget("key1", "key2", "key3", "key4");
        System.out.println("mget:" + mget);
    }

    public static void test6(Jedis jedis) {
        System.out.println("append");
        jedis.flushAll();

        jedis.set("key1", "aaa");
        System.out.println("key1=" + jedis.get("key1"));
        jedis.append("key1", "bbb");
        System.out.println("key1=" + jedis.get("key1"));

    }

    public static void test7(Jedis jedis) {
        System.out.println("getrange 和 setrange");
        jedis.flushAll();

        jedis.set("key1", "abcdefghi");
        System.out.println("key1=" + jedis.get("key1"));
        String key1 = jedis.getrange("key1", 0, 5);
        System.out.println("jedis.getrange(key1)=" + key1);

        jedis.setrange("key1", 3, "xxxxx");
        System.out.println("key1=" + jedis.get("key1"));
    }

    public static void test8(Jedis jedis) {

        System.out.println("setnx");
        jedis.flushAll();
        long n = jedis.setnx("key", "value");
        System.out.println(n);
        String value = jedis.get("key");
        System.out.println(value);
        n = jedis.setnx("key", "value2");
        System.out.println(n);
        value = jedis.get("key");
        System.out.println(value);

    }

    public static void test9(Jedis jedis) {
        System.out.println("psetex");
        jedis.flushAll();
        jedis.psetex("key", 1000, "value");
        long key = jedis.pttl("key");
        System.out.println("key=" + key);
    }


    public static void test10(Jedis jedis) {
        System.out.println("incr 和 decr");
        jedis.flushAll();

        jedis.set("key1", "11");
        System.out.println("key1=" + jedis.get("key1"));
        jedis.incr("key1");
        System.out.println("jedis.incr=" + jedis.get("key1"));
        jedis.decr("key1");
        System.out.println("jedis.decr=" + jedis.get("key1"));
    }


    public static void test11(Jedis jedis) {
        System.out.println("incrby 和 decrby");
        jedis.flushAll();

        jedis.set("key1", "11");
        System.out.println("key1=" + jedis.get("key1"));
        jedis.incrBy("key1",20);
        System.out.println("jedis.incr=" + jedis.get("key1"));
        jedis.decrBy("key1",10);
        System.out.println("jedis.decr=" + jedis.get("key1"));
    }

    //列表操作
    public static void test12(Jedis jedis) {
        System.out.println("lpush 和 lpop");//头插
        jedis.flushAll();

        long n = jedis.lpush("key1", "l2", "l3", "l4");
        System.out.println(n);
        System.out.println("key1=" + jedis.lpop("key1"));
        System.out.println("key1=" + jedis.lpop("key1"));
        System.out.println("key1=" + jedis.lpop("key1"));
        System.out.println("key1=" + jedis.lpop("key1"));

    }


    public static void test13(Jedis jedis) {
        System.out.println("rpush 和 rpop");//尾插
        jedis.flushAll();

        long n = jedis.rpush("key1", "l2", "l3", "l4");
        System.out.println(n);
        System.out.println("key1=" + jedis.rpop("key1"));
        System.out.println("key1=" + jedis.rpop("key1"));
        System.out.println("key1=" + jedis.rpop("key1"));
        System.out.println("key1=" + jedis.rpop("key1"));

    }


    public static void test14(Jedis jedis) {
        System.out.println("lrange");//尾插
        jedis.flushAll();

        jedis.rpush("key1", "1","2","3","4","5","6","7","8","9");
        List<String> list=jedis.lrange("key1", 0, 2);
        System.out.println(list);
    }

    public static void test15(Jedis jedis) {
        System.out.println("blpop");//尾插
        jedis.flushAll();

        while(true) {
            List<String> key = jedis.blpop(100, "key");
            System.out.println(key.get(0));
            System.out.println(key.get(1));
        }
    }

    public static void test16(Jedis jedis){
        System.out.println("linsert");
        jedis.flushAll();

        jedis.lpush("key","1","2","3","4");
        jedis.linsert("key", ListPosition.AFTER,"3","100");
        List<String> values = jedis.lrange("key", 0, -1);
        System.out.println(values);
    }

    public static void test17(Jedis jedis){
        System.out.println("sismember 和 srem");
        jedis.flushAll();

        jedis.sadd("key", "aaa", "bbb", "ccc");
         boolean ok = jedis.sismember("key", "aaa");
         System.out.println(ok);
         long n = jedis.srem("key", "aaa", "bbb");
         System.out.println(n);
        Set<String> set=jedis.smembers("key");
        System.out.println(set);

        ok = jedis.sismember("key", "aaa");
         System.out.println(ok);
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            test17(jedis);
//            test16(jedis);
//            test15(jedis);
//            test14(jedis);
//            test13(jedis);
//            test(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
//            test6(jedis);
//            test7(jedis);
//            test8(jedis);
//            test9(jedis);
//            test10(jedis);
//            test11(jedis);
//            test12(jedis);
        }
    }
}
